/*********************************************************
 * DXUTMgcTerrain.h
 * (C) 2010 David Teeder.
 *********************************************************
 * Class file for DXUT version of MgcTest Terrain
 **********************************************************/

#ifndef _DXUTMGCTERRAIN_H_
#define _DXUTMGCTERRAIN_H_
#include <xnamath.h>
#include "DXUTcamera.h"
#include "PrecompiledHeader.h"
#include "Shader.h"
#include "VertexDecl.h"
#include "Buffer.h"
#include "VertexStructs.h"
#include "Pipeline.h"
using namespace d3d11;

#include <MgcEngine.pkg>
#include <MgcTerrain.pkg>

namespace Mgc
{

	class DxutMgcTerrain //: public MgcApplication
	{
	public:
		DxutMgcTerrain(CFirstPersonCamera *pCamera);
		~DxutMgcTerrain();
		void Render();
		MgcCamera* GetCamera();
		void SetTurretAxes();
		void SetTurretAxes (const MgcVector3& rkLeft, const MgcVector3& rkUp, const MgcVector3& rkDirection);
		void MoveForward();
		void MoveBackward();
		void MoveUp();
		void MoveDown();
		void TurnLeft();
		void TurnRight();
		void LookUp();
		void LookDown();
		WCHAR * GetStats(WCHAR * strText);
		void VertexDataToFile();
		void DxutMgcTerrain::UpdateMgcCamera();

	protected:
		void GetMesh(bool bSaveToFile);
		void RenderText();
		// Terrain functions
		void CreateTerrain();
		void Simplify();

	private:
		void Init();
	
	public:
		D3DXMATRIX				m_matWorld;
		D3DXMATRIX				m_matView;
		D3DXMATRIX				m_matProjection;	
		d3d11::Buffer*			m_pVertices;
		d3d11::Buffer*			m_pIndices;
		d3d11::Pipeline*		m_pPipeline;
		Buffer*			m_pNeverChanges;
		Buffer*			m_pChangesOnResize;
		Buffer*			m_pChangesEveryFrame;
	protected:
		// terrain model
		MgcNodePtr m_spkScene;
		MgcTerrainPagePtr m_spkPage;

		// terrain motion
		MgcReal m_fHeightAboveTerrain;

		// camera
		MgcCameraPtr m_spkCamera;
		CFirstPersonCamera* m_pCamera;
		
		// terrain render stuff
		bool m_bTurretActive;
		bool m_bUpdateView;
		MgcVector3 m_akAxis[3];
		MgcReal m_fTrnSpeed;
		MgcReal m_fRotSpeed;

	private:
		IDirect3DDevice9		*m_pD3DDevice9;
		ID3D11Device			*m_pD3DDevice11;
		unsigned int			m_uiVertexCount;
		uint					*m_pTriangleIndices;
//		VPosColourNormal		*m_pTriangleVertices;
		VPosMultiTex			*m_pTriangleVertices;
		ID3D11ShaderResourceView *m_pRVGrass;
		ID3D11ShaderResourceView *m_pRVRock;
		ID3D11ShaderResourceView *m_pRVSand;
		ID3D11ShaderResourceView *m_pRVSnow;


	};
}
#endif

